package com.dreamcode.caloriestracker.activities;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.dreamcode.caloriestracker.collectors.ProfileDAO;
import com.dreamcode.caloriestracker.collectors.ProfileDAOimpl;
import com.dreamcode.caloriestracker.collectors.SportListAdapter;
import com.dreamcode.caloriestracker.modelclass.ProfileDetails;
import com.dreamcode.caloriestracker.utils.CalorieUtils;

public class RealTimeWorkoutActivity extends Activity {

	private static Spinner groupSpinner = null;	
	private Button back = null;	
	private Button startBtn = null;
	private TextView calculateCal= null;
	private TextView calculateMin= null;
	private TextView calculateSec= null;
	private TextView textView = null;
	private Timer timer = null;
	private ProfileDAO profileDAO = null;	
	private ProfileDetails profileDetails = null;	
		
	TaskTimer taskTimer = null;	
	Handler handler = null;
	double counter = 0;
	int caloriesCalculated = 0;
	int cnt = 0;
	 @Override
     public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.realtimeworkout);
        initView();
     }
	 
	private void initView() {	
		profileDAO = ProfileDAOimpl.getInstance();
		profileDetails = profileDAO.getProfileDetails(RealTimeWorkoutActivity.this, 0);
		handler = new Handler();
		textView = (TextView) findViewById(R.id.value_check);
		calculateCal = (TextView) findViewById(R.id.calculate_cal);
		calculateCal.setTextColor(Color.BLACK);
		calculateMin = (TextView) findViewById(R.id.calculate_min);
		calculateMin.setTextColor(Color.BLACK);
		calculateSec = (TextView) findViewById(R.id.calculate_sec);
		calculateSec.setTextColor(Color.BLACK);
		groupSpinner = (Spinner) findViewById(R.id.group_selector);	
		groupSpinner.setAdapter(new SportListAdapter(RealTimeWorkoutActivity.this, CalorieUtils.getSports()));
		startBtn = (Button) findViewById(R.id.btn_start);
		startBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				calculateCal.setText("");
				calculateMin.setText("");
				calculateSec.setText("");
				timer = new Timer();
				taskTimer = new TaskTimer();
				timer.schedule(taskTimer, 0, 1000);				
				startBtn.setEnabled(false);
				displayProgress();				
			}});
			
		back = (Button) findViewById(R.id.btn_back);
		back.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				finish();
			}
			
		});
	}
	
	public void displayProgress(){
		Builder pgDialog = null;
		ProgressBar pBar = null;	
		LinearLayout pgHolder = new LinearLayout(RealTimeWorkoutActivity.this);
		LinearLayout mainHolder = new LinearLayout(RealTimeWorkoutActivity.this);
		TextView messageHolder = new TextView(RealTimeWorkoutActivity.this);
		messageHolder.setText("   Processing data...");
		mainHolder.setGravity(Gravity.CENTER);
		pgHolder.setLayoutParams(new LinearLayout.LayoutParams(48, 48));
		pBar = new ProgressBar(RealTimeWorkoutActivity.this);	
		pgHolder.addView(pBar);
		mainHolder.addView(pgHolder);
		mainHolder.addView(messageHolder);
		pgDialog = new AlertDialog.Builder(RealTimeWorkoutActivity.this);
		pgDialog.setTitle("Calories Tracker").setView(mainHolder).setNeutralButton("Finish", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Log.i("Min: ",String.valueOf((cnt/60)));
				Log.i("Sec: ",String.valueOf((cnt%60)));
				Log.i("Cal: ",String.valueOf(caloriesCalculated));
				calculateCal.setText(String.valueOf(caloriesCalculated));
				calculateMin.setText(String.valueOf((cnt/60)));
				calculateSec.setText(String.valueOf((cnt%60)));
				timer.cancel();							
				startBtn.setEnabled(true);		
				cnt = 0;
				caloriesCalculated = 0;
				counter = 0;
				dialog.dismiss();
				
			}
		}).show();
		
		
	}
	
	class TaskTimer extends TimerTask{		

		@Override
		public void run() {			
			caloriesCalculated = Integer.parseInt(CalorieUtils.calculateCaloriesByTime(groupSpinner.getSelectedView().getId(), Double.parseDouble(profileDetails.getProfileWeight()), (counter/60)));			
			Log.i("Min: ",String.valueOf((cnt/60)));
			Log.i("Sec: ",String.valueOf((cnt%60)));
			Log.i("Cal: ",String.valueOf(caloriesCalculated));
			counter++;
			cnt++;
		}
		
	}
}
	


